const app = getApp()

Page({
    data: {
        url: app.globalData.url,
        folders: undefined,
        isShow: false,
        user: undefined
    },
    onShow() {
    },
    onLoad() {
        const user = wx.getStorageSync("user")
        if (user) {
            this.setData({ user })
            this.getData()
        }
    },
    // 获取当前用户的所有文件夹
    getData() {
        const that = this
        wx.request({
            url: this.data.url + "/file/show",
            data: { userid: that.data.user.id },
            success(res) {
                let folders = res.data.result
                that.setData({ folders })
            },
        })
    },
    // 打开文件夹
    openFolder(e) {
        const that = this
        wx.redirectTo({
            url: '/pages/image/index?folder=' + e.detail + "&userid=" + that.data.user.id,
        })
    },
    // 删除整个文件夹
    deleteFolder(e) {
        const that = this
        wx.showModal({
            title: '提示',
            content: '确认删除' + e.detail.folder + '吗？',
            success(res) {
                if (res.confirm) {
                    wx.request({
                        url: that.data.url + "/file/deleteFolder",
                        data: {
                            folder: e.detail.folder
                        },
                        success(res) {
                            if (res.data.code === 1) {
                                wx.showToast({ title: '删除成功' })
                                let folders = that.data.folders
                                folders.splice(e.detail.index, 1)
                                that.setData({ folders: folders.length == 0 ? null : folders })
                            }
                        }
                    })
                } else if (res.cancel) {
                    wx.showToast({ title: '取消删除', icon: "error" })
                }
            }
        })
    },
    // 上传操作
    upload() {
        const that = this;
        const appid = "wx8efd630c320af804"
        const secret = "1174ac6e704e3ca93e12eb5b5eeec336"
        // 如果没有登录，需要先登录
        if (!this.data.user) {
            wx.showModal({
                title: '警告',
                content: '请先登录',
                success(res) {
                    if (res.confirm) {
                        wx.login({
                            success(res) {
                                if (res.code) {
                                    // 将登录凭证发送到服务端
                                    wx.request({
                                        url: 'https://api.weixin.qq.com/sns/jscode2session?appid='
                                            + appid + '&secret=' + secret + '&js_code=' + res.code
                                            + '&grant_type=authorization_code',
                                        method: 'GET',
                                        success(res) {
                                            wx.request({
                                                url: that.data.url + "/user/login",
                                                data: {
                                                    id: res.data.openid
                                                },
                                                success(res) {
                                                    if (res.data.code == 1) {
                                                        console.log(res.data.result);

                                                        wx.setStorageSync("user", res.data.result)
                                                        that.setData({ user: res.data.result })
                                                        wx.showToast({ title: '登录成功' })
                                                        const pages = getCurrentPages()
                                                        const perpage = pages[pages.length - 1]
                                                        perpage.onLoad()
                                                    }
                                                }
                                            })
                                        }
                                    })
                                }
                            }
                        })
                    } else if (res.cancel) {
                        wx.showToast({ title: '取消登录' })
                    }
                }
            })
        } else {
            wx.chooseMedia({
                success(r) {
                    const filePaths = r.tempFiles
                    wx.showLoading({
                      title: '请稍后...',
                    })
                    filePaths.forEach(filePath => {
                        wx.uploadFile({
                            filePath: filePath.tempFilePath,
                            name: 'file',
                            url: that.data.url + "/file/upload",
                            formData: { userid: that.data.user.id },
                            success(res) {
                                const data = JSON.parse(res.data)
                                if (data.code === 1) {
                                    wx.hideLoading({
                                      success: (res) => {wx.showToast({ title: '上传成功' })},
                                    })
                                    
                                } else {
                                    wx.showToast({
                                        title: '图片超过2M',
                                        icon: 'error'
                                    })
                                }
                                const pages = getCurrentPages()
                                const perpage = pages[pages.length - 1]
                                perpage.onLoad()
                            }
                        })
                    })
                }
            })
        }
    }
})
